package ModelLayer;
import java.util.ArrayList;
/**
 * 
 * @Author (Alexander Hvidberg, Dennis Kirkebak & Thomas Kulmbach)
 * @Team (Group 1)
 * @Version (08/11/13)
 * 
 */

public class AddressBook
{
    // instance variables - replace the example below with your own
    private ArrayList<Person> persons;
    private int lastUsedId;
    private static AddressBook instance;
    /**
     * Constructor for objects of class AddressBook
     */
    private AddressBook()
    {
        persons = new ArrayList<Person>();
        lastUsedId = 1;
    }

    public static AddressBook getInstance(){
        if(instance == null){
            instance = new AddressBook();
        }
        return instance;
    }
    
    /**
     * Create a new person and add this person to the list of persons.
     * If the phonenumber already exist, print a warning message
     * @param String name - The name of the person
     * @param String address - The address of the person
     * @param String postalCode - The postal code of the person
     * @param String city - The city the person lives in
     * @param String phoneNumber - The persons phone number
     */
    public void createPerson(String name, String address, String postalCode, String city, String phoneNumber)
    {
        if(findPersonByPhoneNumber(phoneNumber)!=null){
            System.out.println("Duplicate person warning! A person with that phone number already exist.");
        }
        else{
            persons.add(new Person(lastUsedId, name, address, postalCode, city, phoneNumber));
            System.out.println("The person has been created");
            lastUsedId++;
        }
    }
    
    /**
     * Finds the person with the given ID
     * 
     * @param int id - The ID to search for
     * @return Person - The object of the type Person, that has the given ID
     */
    public Person findPersonById(int id){
        for(int i = 0; i<persons.size(); i++){
            if(persons.get(i).getId()==id){
                return persons.get(i);
            }
       }
          return null;
    }
    
    /**
     * Finds the person with the given name
     * 
     * @param String name - The name of the person wanted
     * @return Person - The object of the type Person, that has the given name
     */
    public Person findPersonByName(String name){
        for(int i = 0; i<persons.size(); i++){
            if(persons.get(i).getName().equals(name)){
                return persons.get(i);
            }
        }
        return null;
    }
    
    /**
     * Finds the person with the given phone number
     * 
     * @param String phoneNumber - The phone number of the person wanted
     * @return Person - The object of the type Person, that has the given phone number
     */
    public Person findPersonByPhoneNumber(String phoneNumber){
        for(int i = 0; i<persons.size(); i++){
            if(persons.get(i).getPhoneNumber().equals(phoneNumber)){
                return persons.get(i);
            }
            
        }
        return null;
    }
    
    /**
     * Deletes the person(s) with the given ID
     * @param int id - The ID of the person to be deleted
     */
    public void deletePersonById(int id){
        Person p = findPersonById(id);
        if(p!=null){
            persons.remove(p);
            System.out.println("The person has been deleted");
        }
    }
    
    /**
     * Deletes the person(s) with the given phone number
     * @param String phoneNumber - The phone number of the person to be deleted
     */
    public void deletePersonByPhoneNumber(String phoneNumber){
        for(Person person : persons){
            if(person.getPhoneNumber().equals(phoneNumber)){
                persons.remove(person);
                System.out.println("The person: " + person.getName() + " has been deleted");
            }
        }
    }
    
    /**
     * Prints a list of details about all person in the collection
     * 
     * @return String list - the list of details
     */
    public String printDetailList(){
        String list = "";
        for(Person person : persons){
            list = list + person.printShortDetails();
        }
        return list;
    }
    
    /**
     * Updates the information about a person in the collection
     * 
     * @param int id - the id of the person
     * @param String name - the name of the person
     * @param String address - the address of the person
     * @param String postalCode - the postalcode of the person
     * @param String city - the city the person lives in
     * @param String phoneNumber - the phonenumber of the person
     */
    public void updatePerson(int id, String name, String address, String postalCode, String city, String phoneNumber){
        if(findPersonById(id)!=null){
            findPersonById(id).updatePerson(name, address, postalCode, city, phoneNumber);
            System.out.println("The person has been updated");
        }
        else{
            System.out.println("The person was not found");
        }
    }
  //Set & Get methods for the entire class
    /**
     * Returns a list of person in the collection
     * 
     * @return ArrayList persons - a list of persons
     */
    public ArrayList getPersonList(){
        return persons;
    }
}
